package com.lagou.dao;


import com.lagou.bean.Classe;
import com.lagou.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClassDaoImp implements ClassDao {


    @Override
    public List<Classe> getCla() {
        List<Classe> classes = new ArrayList<Classe>();
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        ResultSet rst = null;
        String sql = "select * from class ";
        try {
            pst = conn.prepareStatement(sql);

            rst = pst.executeQuery();
            while (rst.next()) {
                Classe cla = new Classe();
                cla.setCid(rst.getString("cid"));
                cla.setGrade(rst.getInt("grade"));
                cla.setHeadMaster(rst.getString("headMaster"));
                cla.setSlogan(rst.getString("slogan"));
                cla.setNum(rst.getInt("num"));
                classes.add(cla);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,pst,rst);
        }
        return classes;
    }


    @Override
    public boolean addCla(Classe classe) {
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        String sql = "insert into class(cid,grade,headMaster,slogan,num) values (?,?,?,?,?)";;

        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, classe.getCid());
            pst.setInt(2, classe.getGrade());
            pst.setString(3, classe.getHeadMaster());
            pst.setString(4, classe.getSlogan());
            pst.setInt(5, classe.getNum());
            int count = pst.executeUpdate();
            return count > 0 ? true : false;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,pst);
        }
        return false;
    }


    @Override
    public boolean updateCla(Classe classe) {
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        String sql = "update class set grade = ?, headMaster = ? ,slogan = ?, num = ? where cid = ?";

        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1, classe.getGrade());
            pst.setString(2, classe.getHeadMaster());
            pst.setString(3, classe.getSlogan());
            pst.setInt(4, classe.getNum());
            pst.setString(5, classe.getCid());
            int count = pst.executeUpdate();
            return count > 0 ? true : false;
         } catch (SQLException e) {
             e.printStackTrace();
         } finally {
            DBUtil.close(conn,pst);
        }

        return false;
    }


    @Override
    public boolean delCla(Classe classe) {
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        String sql = "delete from class where cid = ?";

        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, classe.getCid());
            int count = pst.executeUpdate();
            return count > 0 ? true : false;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,pst);
        }

        return false;

    }

    @Override
    public boolean isExsistStd(String classNo) {
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        ResultSet rst = null;
        String sql = "select * from student where cid = ?";

        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, classNo);
            rst = pst.executeQuery();
            if(rst.next()){
                return true;
            }
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,pst);
        }

        return true;

    }

}
